package com.haohuo.question1;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 简单用户类
 */
public class User {
    private String username;
    private Map<String, Integer> localLables = new ConcurrentHashMap<String, Integer>();

    public User(String username) {
        this.username = username;
    }

    /**
     * 用户点击内容
     *
     * @param content
     */
    public void checkContent(Content content) {
        if (content == null) {
            return;
        }
        List<String> labels = content.getLabels();
        for (String label : labels) {
            Integer sum = localLables.get(label);
            if (sum == null) {
                localLables.put(label, 1);
            } else {
                localLables.put(label, new AtomicInteger(sum).incrementAndGet());
            }
        }


    }

    public String out() {
        StringBuffer buffer = new StringBuffer();
        Set<Map.Entry<String, Integer>> entries = localLables.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            buffer.append(String.format("%s:%s,", next.getKey(), next.getValue()));
        }
        if (entries.size() > 0) {
            buffer.deleteCharAt(buffer.toString().length() - 1);
            buffer.append(")");
            return "(" + buffer;
        }
        return buffer.toString();
    }

    public String getUsername() {
        return username;
    }
}
